package cn.wit.summit.chwan.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by Alan on 2018/9/7.
 */

@SuppressLint("AppCompatCustomView")
public class DigitalChangeTextView extends TextView implements Runnable{
    private static final int COUNT = 80;
    private int mTextColor = 0xF3E5C9;
    private float mValue = 0.0f;
    private float mAverage = 0.0f;
    private int mCount = 0;

    public DigitalChangeTextView(Context context) {
        this(context, null);
    }

    public DigitalChangeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mAverage = mValue / COUNT;
        setTextColor(mTextColor);
        setText(String.valueOf(0.0f));
    }

    public double getValue() {
        return mValue;
    }

    public void setValue(float mValue) {
        mAverage = mValue / COUNT;
    }

    public int getTextColor() {
        return mTextColor;
    }

    public void setTextColor(int mTextColor) {
        this.mTextColor = mTextColor;
    }

    public void runView() {
        mCount = 0;
        mValue = 0.0f;
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        while (mCount < COUNT) {
            mCount++;
            mValue += mAverage;
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            post(new Runnable() {
                @Override
                public void run() {
                    setText(String.format("%.2f", mValue));
                }
            });
        }
    }
}
